package com.lac.component.cas;

/**
 * @author xiaokuixiao
 * date2025/7/9 13:49
 */
import java.util.concurrent.atomic.AtomicInteger;

public class VersionedInteger {
    private int value;
    private AtomicInteger version = new AtomicInteger(0);

    public VersionedInteger(int initialValue) {
        this.value = initialValue;
    }

    public boolean compareAndSwap(int expectedValue, int newValue, int expectedVersion) {
        if (value == expectedValue && version.get() == expectedVersion) {
            value = newValue;
            version.incrementAndGet(); // 增加版本号
            return true;
        }
        return false;
    }
}
